package com.shardingsphere.shardingsphere.controller;

import com.shardingsphere.shardingsphere.domain.Project;
import com.shardingsphere.shardingsphere.domain.ProjectUser;
import com.shardingsphere.shardingsphere.service.ProjectService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = "/project")
public class ProjectController {

	@Autowired
	ProjectService projectService;




	@GetMapping(value = "/add")
	public  void addProject(){
		Project project=new Project();
		project.setId(UUID.randomUUID().toString().replace("-",""));
		project.setProjectCode("HH-2019-0001");
		project.setProjectName("菜菜测试项目");
		LocalDateTime date= LocalDateTime.parse("2018-09-01 14:11:15", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		ZoneId zone = ZoneId.systemDefault();
		project.setCreateTime(new Timestamp(date.atZone(zone).toInstant().toEpochMilli()));
		projectService.save(project);
	}

	@GetMapping(value = "/list")
	public List<Project> getProjectList(){
		return projectService.getProjectList();
	}

	@GetMapping(value = "/{id}")
	public Project getProjectById(@PathVariable String id){
		return projectService.getProjectById(id);
	}


	@GetMapping(value = "/addProjectUser")
	public void addProjectUser(){
		ProjectUser projectUser=new ProjectUser();
		projectUser.setId(UUID.randomUUID().toString().replace("-",""));
		projectUser.setProjectId("873ca75123ee4caebaac3e404dd3a416");
		projectUser.setProjectUser("菜菜2");
		LocalDateTime date= LocalDateTime.parse("2018-09-01 14:11:15", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		ZoneId zone = ZoneId.systemDefault();
		projectUser.setCreateTime(new Timestamp(date.atZone(zone).toInstant().toEpochMilli()));
		projectService.addProjectUser(projectUser);

	}

	@GetMapping(value = "/projectuser/{id}")
	public  ProjectUser getProjectUser(@PathVariable String id){
		return projectService.getProjectUser(id);

	}




}
